package servergui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import clientserver.Server;
import java.awt.Font;

/**
 * Class handles the GUI for the server-side program.
 * @author Group11
 *
 */
public class ServerGUI extends JFrame {

	private JPanel contentPane;
	private JTextField adminField;
	private JTextField sqlpasswordField;
	private JTextField portfield;
    //Server variables .....
	private Server server;
	private JTextField sqlipfield;
	private String localip;
	private JLabel localiplbl;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ServerGUI frame = new ServerGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ServerGUI() {
		try {
			localip=InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 453, 495);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		
		localiplbl = new JLabel("Server Ip:"+localip);
		localiplbl.setFont(new Font("Tahoma", Font.PLAIN, 13));
		localiplbl.setForeground(new Color(135, 206, 235));
		GridBagConstraints gbc_localiplbl = new GridBagConstraints();
		gbc_localiplbl.insets = new Insets(0, 0, 5, 5);
		gbc_localiplbl.gridx = 0;
		gbc_localiplbl.gridy = 0;
		contentPane.add(localiplbl, gbc_localiplbl);
		
		final JLabel lblStatus = new JLabel("Status: Offline");
		lblStatus.setForeground(Color.RED);
		GridBagConstraints gbc_lblStatus = new GridBagConstraints();
		gbc_lblStatus.insets = new Insets(0, 0, 5, 5);
		gbc_lblStatus.gridx = 4;
		gbc_lblStatus.gridy = 1;
		contentPane.add(lblStatus, gbc_lblStatus);
		
		JLabel lblMysqlAdminLogin = new JLabel("MYSQL Admin Login: ");
		GridBagConstraints gbc_lblMysqlAdminLogin = new GridBagConstraints();
		gbc_lblMysqlAdminLogin.insets = new Insets(0, 0, 5, 5);
		gbc_lblMysqlAdminLogin.gridx = 4;
		gbc_lblMysqlAdminLogin.gridy = 2;
		contentPane.add(lblMysqlAdminLogin, gbc_lblMysqlAdminLogin);
		
		adminField = new JTextField();
		adminField.setText("root");
		GridBagConstraints gbc_adminField = new GridBagConstraints();
		gbc_adminField.insets = new Insets(0, 0, 5, 5);
		gbc_adminField.gridx = 5;
		gbc_adminField.gridy = 2;
		contentPane.add(adminField, gbc_adminField);
		adminField.setColumns(10);
		
		JLabel lblMysqlIp = new JLabel("MySQL IP:");
		GridBagConstraints gbc_lblMysqlIp = new GridBagConstraints();
		gbc_lblMysqlIp.anchor = GridBagConstraints.EAST;
		gbc_lblMysqlIp.insets = new Insets(0, 0, 5, 5);
		gbc_lblMysqlIp.gridx = 4;
		gbc_lblMysqlIp.gridy = 3;
		contentPane.add(lblMysqlIp, gbc_lblMysqlIp);
		
		sqlipfield = new JTextField();
		sqlipfield.setText("localhost");
		GridBagConstraints gbc_sqlipfield = new GridBagConstraints();
		gbc_sqlipfield.insets = new Insets(0, 0, 5, 5);
		gbc_sqlipfield.fill = GridBagConstraints.HORIZONTAL;
		gbc_sqlipfield.gridx = 5;
		gbc_sqlipfield.gridy = 3;
		contentPane.add(sqlipfield, gbc_sqlipfield);
		sqlipfield.setColumns(10);
		
		JLabel lblMysqlPassword = new JLabel("MYSQL Password: ");
		GridBagConstraints gbc_lblMysqlPassword = new GridBagConstraints();
		gbc_lblMysqlPassword.insets = new Insets(0, 0, 5, 5);
		gbc_lblMysqlPassword.gridx = 4;
		gbc_lblMysqlPassword.gridy = 4;
		contentPane.add(lblMysqlPassword, gbc_lblMysqlPassword);
		
		sqlpasswordField = new JTextField();
		sqlpasswordField.setText("12345");
		GridBagConstraints gbc_sqlpasswordField = new GridBagConstraints();
		gbc_sqlpasswordField.insets = new Insets(0, 0, 5, 5);
		gbc_sqlpasswordField.gridx = 5;
		gbc_sqlpasswordField.gridy = 4;
		contentPane.add(sqlpasswordField, gbc_sqlpasswordField);
		sqlpasswordField.setColumns(10);
		
		JLabel lblServerPort = new JLabel("Server Port:");
		GridBagConstraints gbc_lblServerPort = new GridBagConstraints();
		gbc_lblServerPort.anchor = GridBagConstraints.EAST;
		gbc_lblServerPort.insets = new Insets(0, 0, 5, 5);
		gbc_lblServerPort.gridx = 4;
		gbc_lblServerPort.gridy = 6;
		contentPane.add(lblServerPort, gbc_lblServerPort);
		
		portfield = new JTextField();
		portfield.setText("5152");
		GridBagConstraints gbc_portfield = new GridBagConstraints();
		gbc_portfield.insets = new Insets(0, 0, 5, 5);
		gbc_portfield.gridx = 5;
		gbc_portfield.gridy = 6;
		contentPane.add(portfield, gbc_portfield);
		portfield.setColumns(10);
		
		JButton btnRunServer = new JButton("Run Server");
		btnRunServer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
		
				String t=portfield.getText();
				String admin=adminField.getText();
				
				String password=sqlpasswordField.getText();
				int port =Integer.parseInt(t);
				String sip=sqlipfield.getText();
					server=new Server(port,admin,password,sip);
				try {
					server.listen();
				} catch (IOException e) {
										e.printStackTrace();
				}
			
				server.connectToMySQL();
				
				lblStatus.setText("Server: Online");
				lblStatus.setForeground(Color.BLUE);
				
			}
		});
		GridBagConstraints gbc_btnRunServer = new GridBagConstraints();
		gbc_btnRunServer.insets = new Insets(0, 0, 5, 5);
		gbc_btnRunServer.gridx = 4;
		gbc_btnRunServer.gridy = 8;
		contentPane.add(btnRunServer, gbc_btnRunServer);
		
		JButton btnStopServer = new JButton("Stop Server");
		btnStopServer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					server.close();
				} catch (IOException e) {
							e.printStackTrace();
				}
				lblStatus.setText("Server: Offline");
				lblStatus.setForeground(Color.RED);
			}
		});
		GridBagConstraints gbc_btnStopServer = new GridBagConstraints();
		gbc_btnStopServer.insets = new Insets(0, 0, 5, 5);
		gbc_btnStopServer.gridx = 5;
		gbc_btnStopServer.gridy = 8;
		contentPane.add(btnStopServer, gbc_btnStopServer);
	}

}
